package com.src.xyzk_personal.util;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DeleteLogUtil {

    private static final String LOG_FOLDER_PATH = "/storage/emulated/0/EOL_DOWNLINE/LOG/";

    public static void deleteOldLogFolders(int days) {
        // 获取log文件夹的路径
        File logFolder = new File(LOG_FOLDER_PATH);

        // 检查log文件夹是否存在
        if (logFolder.exists() && logFolder.isDirectory()) {
            // 获取当前日期
            Calendar calendar = Calendar.getInstance();
            Date currentDate = calendar.getTime();

            // 计算过去指定天数的日期
            calendar.add(Calendar.DAY_OF_MONTH, -days);
            Date targetDate = calendar.getTime();

            // 获取log文件夹中的所有文件夹（每天一个文件夹）
            File[] logDateFolders = logFolder.listFiles(new FileFilter() {
                @Override
                public boolean accept(File pathname) {
                    return pathname.isDirectory();
                }
            });

            if (logDateFolders != null) {
                for (File logDateFolder : logDateFolders) {
                    try {
                        // 解析文件夹名称为日期
                        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd", Locale.getDefault());
                        Date folderDate = dateFormat.parse(logDateFolder.getName());

                        if (folderDate != null && folderDate.before(targetDate)) {
                            // 删除指定日期之前的日志文件夹
                            deleteFolder(logDateFolder);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    private static void deleteFolder(File folder) {
        if (folder.isDirectory()) {
            File[] files = folder.listFiles();
            if (files != null) {
                for (File file : files) {
                    // 递归删除文件夹中的所有文件
                    deleteFolder(file);
                }
            }
        }
        // 删除空文件夹或文件
        folder.delete();
    }

    // 删除过去七天
    public static void deleteLastSevenDaysLogs() {
        deleteOldLogFolders(7);
    }

    // 删除过去三十天之前的日志
    public static void deleteLastThirtyDaysLogs() {
        deleteOldLogFolders(30);
    }

    // 删除过去半年之前的日志
    public static void deleteLastSixMonthsLogs() {
        // 由于每个月的天数不固定，这里采用近似计算，大约180天
        deleteOldLogFolders(180);
    }

    // 时间不限-全删
    public static void deleteAllLogs() {
        // 获取log文件夹的路径
        File logFolder = new File(LOG_FOLDER_PATH);

        // 检查log文件夹是否存在
        if (logFolder.exists() && logFolder.isDirectory()) {
            // 删除log文件夹中的所有文件和文件夹
            deleteFolder(logFolder);
        }
    }

    public static void createTestLogs() {
        createLogsForDays(7);
        createLogsForDays(30);
        createLogsForDays(180);
    }

    private static void createLogsForDays(int days) {
        Calendar calendar = Calendar.getInstance();
        for (int i = 0; i < days; i++) {
            Date currentDate = calendar.getTime();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd", Locale.getDefault());
            String folderName = dateFormat.format(currentDate);
            createLogFolder(folderName);
            calendar.add(Calendar.DAY_OF_MONTH, -1); // 向前推移一天
        }
    }

    private static void createLogFolder(String folderName) {
        File logFolder = new File(LOG_FOLDER_PATH + folderName);
        if (!logFolder.exists()) {
            logFolder.mkdirs(); // 创建日志文件夹
            // 创建一个测试日志文件
            try {
                File logFile = new File(logFolder, "test_log.txt");
                logFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
